﻿//新增
function add(pid) {
    win.open("/gx/sysdictionary/addedit?pid=" + pid, 550, 450, "新增");
}
//编辑
function edit(id) {
    win.open("/gx/sysdictionary/addedit?id=" + id, 550, 450, "编辑");
}
//删除
function del(id, obj) {
    win.confirm("确认要删除吗？", function () {
        $.post("/gx/sysdictionary/deletedata", { id: id }, function (res) {
            if (res.Code == 200) {
                $(obj).parent().parent().parent().remove();
            } else if (res.Code == 300) {
                win.alert(res.Message);
            }
            else {
                win.alert("删除失败");
            }
        });
    });
}
//父级展开
function onOpen(obj, id) {
    if ($(obj).siblings("i").hasClass("layui-icon-right")) {
        $(obj).siblings("i").removeClass("layui-icon-right").addClass("layui-icon-down");
        $("#nodeContent" + id).addClass("layui-show");
    } else {
        $(obj).siblings("i").removeClass("layui-icon-down").addClass("layui-icon-right");
        $("#nodeContent" + id).removeClass("layui-show");
    }
}
//子级展开
function onNodeOpen(obj, id) {
    if ($(obj).siblings("i").hasClass("layui-icon-right")) {
        $(obj).siblings("i").removeClass("layui-icon-right").addClass("layui-icon-down");
        $("#btnContent" + id).addClass("layui-show");
    } else {
        $(obj).siblings("i").removeClass("layui-icon-down").addClass("layui-icon-right");
        $("#btnContent" + id).removeClass("layui-show");
    }
}
//修改排序
function updateSort(id, obj) {
    $.post("/gx/sysdictionary/updatesort", { id: id, sort: $(obj).val() }, function (res) { });
}
//获取字典
function getModel(item, id) {
    if (id > 0) {
        $("#lbl" + id).html(item.Name);
        $("#lbl" + id).parent().find("input[name='sort']").val(item.Sort);
    } else {
        var dictionaryHtml = "<div class=\"layui-colla-item\">";
        dictionaryHtml += "<h2 class=\"layui-colla-title\">";
        dictionaryHtml += "<i class=\"layui-icon layui-icon-right\"></i>&nbsp;&nbsp;";
        if (item.ParentId == 0) {
            dictionaryHtml += "<label class=\"plbl\" id=\"lbl" + item.Id + "\" >" + item.Name + "</label>";
            dictionaryHtml += "<div class=\"layui-btn-group\" style=\"padding-left:10px;border-left:1px solid #ddd;\">";
            dictionaryHtml += "<button class=\"layui-btn layui-btn-sm\" onclick=\"add(" + item.Id + ")\" title=\"新增\">";
            dictionaryHtml += "<i class=\"layui-icon\">&#xe654;</i>";
            dictionaryHtml += "</button>";
        } else {
            dictionaryHtml += "<label id=\"lbl" + item.Id + "\" >" + item.Name + "</label>";
            dictionaryHtml += "<div class=\"layui-btn-group\" style=\"padding-left:10px;border-left:1px solid #ddd;\">";
        }
        dictionaryHtml += "<button class=\"layui-btn layui-btn-sm\" onclick=\"edit(" + item.Id + ")\" title=\"编辑\">";
        dictionaryHtml += "<i class=\"layui-icon\">&#xe642;</i>";
        dictionaryHtml += "</button>";
        dictionaryHtml += "<button class=\"layui-btn layui-btn-sm\" onclick=\"del(" + item.Id + ",this)\" title=\"删除\">";
        dictionaryHtml += "<i class=\"layui-icon\">&#xe640;</i>";
        dictionaryHtml += "</button>";
        dictionaryHtml += "</div>";
        dictionaryHtml += "<div style=\"display:inline-block;width:80px;\">";
        dictionaryHtml += "<input type=\"number\" placeholder=\"排序\" class=\"layui-input\" style=\"height:30px;\" value=\"" + item.Sort + "\" onchange=\"updateSort(" + item.Id + ",this)\" />";
        dictionaryHtml += "</div>";
        dictionaryHtml += "</h2>";
        dictionaryHtml += "</div>";
        if (item.ParentId > 0) {
            $("#nodeContent" + item.ParentId + " .layui-collapse").append(dictionaryHtml);
        } else {
            window.location.reload();
        }
    }
}